//使用成员函数（member function）方式，实现了运算符+=、—（取负）重载
//使用友元（friend）函数方式，实现了运算符 —（减法）重载
#include <iostream>
using namespace std;
class Complex
{
public:
    Complex(double r=0,double i=0):re(r),im(i) {}
    void print()
    {
        cout<< re <<" + " << im <<"i\n";
    }

    Complex& operator+=(const Complex& cm)
    {
        re+=cm.re;
        im+=cm.im;
        return *this;
    }
    Complex operator-()
    {
        return Complex(-re, -im );
    }

    friend Complex operator-(Complex cm1,Complex cm2);

private:
    double re;//实部
    double im; //虚部
};

Complex operator-(Complex cm1,Complex cm2)
{
    Complex cm;
    cm.re = cm1.re - cm2.re;
    cm.im = cm1.im - cm2.im;
    return cm;
}

int main()
{
    Complex cm1(1.5,-2.3),cm2(3.3,8.7),cm3,cm4,cm5(1,1);
    cm3 = -cm1;
    cm3.print();
    cm4 = cm2 - cm1;
    cm4.print();
    cm2 +=cm1;
    cm2.print();
    cm5 +=cm1 +=cm1;
    cm5.print();
    return 0;
}
